<template>
    <view class="container" :style="themeColor">
        <header-bar :config="config" @rightClick="rightClick"></header-bar>
        <menu-bar />
        <menu-filter @doFilter="doFilter" />
        <view class="main">
            <mescroll-uni :height="height" ref="mescrollRef" @init="mescrollInit" :down="downOption" @down="refresh"
                :up="upOption" @up="getData" @emptyclick="emptyClick">
                <cart-item v-if="isLoadOk" v-for="(item, index) in datas" :key="item.OrderId" :selectData="selectData"
                    :productInfo="item" @upNum="upNum" @changeCheck="changeCheck">{{ item.SupplierName }}</cart-item>
            </mescroll-uni>
        </view>
        <order-bottom-bar ref="orderBottomBar" :isHome="isHome" :selectData="selectData" :productInfo="datas"
            @selectAll="selectAll" :totalPrice="totalPrice" @isSelectAllBack="isSelectAllBack"></order-bottom-bar>
    </view>
</template>

<script>
import menuBar from "@/pages_me/components/billing/menu-bar.vue"
import menuFilter from "@/pages_me/components/billing/menu-filter.vue"
import cartItem from "@/pages_me/components/billing/cart-item.vue"
import orderBottomBar from "@/pages_me/components/billing/order-bottom-bar.vue"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
import { getUserOrderList, GetOrderInvoiceList } from "@/service/api/order.js"
export default {
    components: {
        menuBar,
        menuFilter,
        cartItem,
        orderBottomBar,
    },
    props: {
        isHome: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            config: {
                title: "开票中心",
                back: true,
                centerSlot: true,
                barPlaceholder: true,
                rightSlot: true,
                rightBtns: [
                    {
                        id: 1,
                        btnType: 1,
                        type: "填写发票信息",
                    },
                ],
            },
            optEmpty: {
                icon: "/static/img/empty.png", // 图标路径 (建议放入static目录, 如 /static/img/mescroll-empty.png )
                tip: "~ 暂无数据 ~", // 空提示
            },
            // upOption: {
            //     page: {
            //     	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
            //     	size: 10, // 每页数据的数量
            //     	time: null // 加载第一页数据服务器返回的时间; 防止用户翻页时,后台新增了数据从而导致下一页数据重复;
            //     },
            //     textLoading: '加载中 ...', // 加载中的提示文本
            //     textNoMore: '-- 没有更多了 --', // 没有更多数据的提示文本
            //     toTop: {
            //     	src: false
            //     },
            //     empty: {
            //     	icon: "/static/img/empty.png" ,// 图标路径 (建议放入static目录, 如 /static/img/mescroll-empty.png )
            //     	tip: '~ 暂无数据 ~' // 空提示
            //     },

            // },
            downOption: {
                use: true,
            },
            isLoadOk: false,
            tabIndex: 0,
            listHeight: 0,
            height: "",
            loadOk: false,
            requestData: {
                startTime: "",
                endTime: "",
                startOrderPrice: "",
                endOrderPrice: "",
            },
            selectData: {
                ids: [],
            },
            isSelectAll: true,
            delPro: null,
            param: {},
            datas: [],
            totalPrice: 0,
        }
    },
    created() {
        if (!this.isHome) {
            this.config.back = true
            this.$nextTick(() => {
                this.getListHeight()
            })
        }
    },
    mounted() {
        this.upBottom()

        if (this.isSelectAll) {
            this.selectAll(true)
        }
        //#ifndef MP
        this.refresh(this.requestData)
        //#endif
    },
    methods: {
        rightClick(data) {
            this.easyGoTo("/pages_me/packet/billing/creatBilling?type=info&form=me")
        },
        //获取列表高度
        getListHeight() {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                e = this.isHome ? e - 54 - uni.upx2px(120) : e - uni.upx2px(120)
                this.height = e + "px"
                this.isLoadOk = true
            })
        },
        //获取列表数据
        getData(page) {
            let param = this.requestData
            param.pageSize = page.size
            param.pageIndex = page.num
            param.orderState = "10"
            param.InvoiceState = "0"
            param.IsInvoice = true
            getUserOrderList(param).then((res) => {
                if (res.State == 1) {
                    this.mescroll.endBySize(res.Data.length, res.RecordAmout)
                    this.datas = this.datas.concat(res.Data)
                } else {
                    this.mescroll.endBySize(0, 0)
                }
            })
        },

        //刷新
        refresh(requestData) {
            setTimeout((e) => {
                this.downRefresh(requestData, true)
            }, 50)
        },
        //刷新
        downRefresh(requestData, isSetParam = false) {
            this.datas = []
            if (isSetParam) {
                this.param = requestData
            }
            this.mescroll.resetUpScroll()
        },
        //筛选
        doFilter(data) {
            this.requestData.startTime = data.startDate ? data.startDate + " 00:00:00" : ""
            this.requestData.endTime = data.endDate ? data.endDate + " 23:59:59" : ""
            this.requestData.startOrderPrice = data.startOrderPrice
            this.requestData.endOrderPrice = data.endOrderPrice
            this.refresh(this.requestData)
        },
        //更新底栏
        upBottom() { },
        //更新底栏数据
        upNum(e) {
            this.upBottom()
        },
        //全选
        selectAll(e) {
            let ids = []
            if (e) {
                this.datas.forEach((pro) => {
                    ids.push(pro.OrderId)
                    this.totalPrice += pro.InvoiceMoney
                })
            } else {
                this.totalPrice = 0
            }
            this.setTreeSelect(ids)
        },
        //数据处理
        setTreeSelect(ids) {
            //去重
            const set = new Set(ids)
            ids = [...set]
            this.selectData = {
                ids: ids,
            }
            this.$forceUpdate()
        },
        //单个商品全选or取消全选
        changeCheck(obj) {
            let isSelect = obj.isSelect
            let OrderId = obj.OrderId
            let PriceSum = obj.PriceSum
            if (isSelect) {
                // 选中
                this.datas.forEach((e) => {
                    if (e.OrderId == OrderId) {
                        this.selectData.ids.push(e.OrderId)
                        this.totalPrice += e.InvoiceMoney
                    }
                })
            } else {
                //取消
                this.selectData.ids.pop()
                this.totalPrice = this.totalPrice - InvoiceMoney
            }
            this.setTreeSelect(this.selectData.ids)
        },
        isSelectAllBack(e) {
            this.isSelectAll = e
        },
        //去采购
        emptyClick() {
            this.easyGoProduct()
        },
    },
}
</script>

<style lang="scss" scoped>
.css-h120 {
    height: 120rpx;
    width: 100%;
}
</style>
